package com.lanou.musicproject.manager;

import android.app.Activity;
import android.content.SharedPreferences;

import com.lanou.musicproject.R;

/**
 * Created by 王世美 on 15/11/3.
 * 皮肤管理类
 */
public class SkinSettingManager {
    public final String SKIN_PREF = "skinSetting";
    private Activity mActivity;
    public SharedPreferences skinSettingPreference;

    /*初始化皮肤资源*/
    private int[] skinResources = {R.mipmap.a0,R.mipmap.a1,R.mipmap.ping,R.mipmap.a3,R.mipmap.a4,R.mipmap.hua
            ,R.mipmap.jing,R.mipmap.a7,R.mipmap.pei,R.mipmap.a9};

    public SkinSettingManager(Activity activity) {
        this.mActivity = activity;
        skinSettingPreference = mActivity.getSharedPreferences(SKIN_PREF,3);
    }

    /*获取当前的皮肤序号
    * */
    public int getSkinType() {
        String key = "skin_type";
        return skinSettingPreference.getInt(key,0);
    }

    /*把皮肤号写到全局设置里去
    * @param j
    * */
    public void setSkinType(int j) {
        SharedPreferences.Editor editor = skinSettingPreference.edit();
        String key = "skin_type";
        editor.putInt(key,j);
        editor.commit();
    }

    /*
    * 获取当前皮肤的背景图资源id
    */
    public int getCurrentSkinRes() {
        int skinLen = skinResources.length;
        int getSkinLen = getSkinType();
        if (getSkinLen >= skinLen) {
            getSkinLen = 0;
        }
        return skinResources[getSkinLen];
    }

    /*选择皮肤资源
    * @param skinTypeIndex 皮肤资源索引
    * */
    public void selectSkinType(int skinTypeIndex) {
        setSkinType(skinTypeIndex);
        mActivity.getWindow().setBackgroundDrawable(null);
        try {
            mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /*
    * 用于初始化
    * */
    public void initSkins() {
        mActivity.getWindow().setBackgroundDrawableResource(getCurrentSkinRes());
    }

}



